/*
 * Copyright 2011 by Brian Dominy <brian@oddchange.com>
 *
 * This file is part of FreeWPC.
 *
 * FreeWPC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * FreeWPC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FreeWPC; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * Duty cycle driver using two Fliptronic coils
 */

@@class duty

@@parameter power
@@parameter time
@@parameter hold

@@
@@file @self.sched
@@
!@self_service 16 20c


@@
@@file @self.h
@@

#include <freewpc.h>

extern U8 @self_timer;

#undef DUTY_FREQ
#define DUTY_FREQ 16
#undef TIME_SCALE
#define TIME_SCALE (16 / DUTY_FREQ)

void @self_start (void);
void @self_stop (void);


/**
 * Realtime update of a duty-cycled device.
 */
extern inline void @self_service (void)
{
	if (unlikely (@self_timer > 0))
	{
		@self_timer--;
		if (@self_timer == 0)
		{
			sol_disable (@power);
			sol_enable (@hold);
		}
	}
}

@@
@@file @self.c
@@

#include <freewpc.h>
#include "@self.h"

/** The number of cycles for which the solenoid is kept on. */
U8 @self_timer;


/**
 * The user API to start a duty-cycled solenoid.
 */
void @self_start (void)
{
	sol_enable (@power);
	@self_timer = @time;
}


/**
 * The user API to stop a duty-cycled solenoid.
 */
void @self_stop (void)
{
	sol_disable (@power);
	sol_disable (@hold);
	@self_timer = 0;
}


CALLSET_ENTRY (@self, init, tilt, stop_game)
{
	@self_stop ();
}

/* vim: set filetype=c: */
